167. 两数之和 II - 输入有序数组
为保证权益,题目请参考 167. 两数之和 II - 输入有序数组(From LeetCode).
解决方案1
CPP
C++
/**
* @brief 求两个数值
*/
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
static vector<int> twoSum(vector<int> &numbers, int target) {
int l = 0;
int r = numbers.size() - 1;
vector<int> res;
while (l < r) {
if (numbers[l] + numbers[r] == target) {
res.push_back(l+1);
res.push_back(r+1);
return res;
} else if (numbers[l] + numbers[r] > target) {
r--;
} else {
l++;
}
}
return res;
}
};
int main() {
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34